//
//  OneDayViewController.m
//  项目
//
//  Created by 张诗雨 on 14/12/29.
//  Copyright (c) 2014年 张诗雨. All rights reserved.
//

#import "OneDayViewController.h"
#import "UIImageView+WebCache.h"
#import "PicOneTableViewCell.h"
#import "UIButton+WebCache.h"
#import "AFNetWoringYu.h"
#import "MJRefresh.h"
#import "WebColor.h"
#import "MJRefreshHeaderView.h"
#import "MJRefreshFooterView.h"
#import "OnedayModel.h"
#import "PlayInfoModel.h"
#import "TingOneTableViewCell.h"
#import "ReadStoryTableViewCell.h"
#import "MusicTableViewCell.h"
#import "MusicPlayViewController.h"
#import "PicOneTableViewCell.h"
#import "ChoiceDetailViewController.h"
#import "Singleton.h"
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
//获取屏幕尺寸的宏
@interface OneDayViewController ()
@property (nonatomic, assign)NSInteger start;
@property (nonatomic, assign)NSInteger number;

@end

@implementation OneDayViewController
- (void)dealloc
{
    [self.tableArr release];
    [self.date release];
    [self.playArr release];
    [self.imgArr release];
    [self.tableView release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArr = [NSMutableArray array];
        self.playArr = [NSMutableArray array];
        self.imgArr = [NSMutableArray array];

    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [WebColor whiteSmoke];
    [self requestData]; // request Data
    [self creatItem]; //leftNavButtonItem
    [self createView];  // 创建视图
    [self headerRereshing];
    [self footerRereshing];
}

- (void)createView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 5, SCREEN_WIDTH, SCREEN_HEIGHT - 5) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorColor=[UIColor clearColor];    //分割cell线颜色
    [self.tableView registerClass:[TingOneTableViewCell class] forCellReuseIdentifier:@"cell1"];
    [self.tableView registerClass:[ReadStoryTableViewCell class] forCellReuseIdentifier:@"cell2"];
    [self.tableView registerClass:[MusicTableViewCell class] forCellReuseIdentifier:@"cell3"];
    [self.tableView registerClass:[PicOneTableViewCell class] forCellReuseIdentifier:@"cell4"];
    [self.view addSubview:self.tableView];
    [_tableView release];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _tableArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OnedayModel *model = [self.tableArr objectAtIndex:indexPath.row];
    if ([model.name isEqualToString:@"音乐"]) {
        MusicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell3"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.typeLable.text = [NSString stringWithFormat:@"--%@--",model.name];
        cell.titleLable.text = model.title;
        cell.contentLable.text = model.content;
        return cell;
        
    } else if ([model.name isEqualToString:@"Ting"]) {
        TingOneTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
        [cell.backImg setImageWithURL:[NSURL URLWithString:model.coverimg]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.typeLable.text = [NSString stringWithFormat:@"--%@--",@"sound"];
        cell.titleLable.text = model.title;
        cell.nameLable.text = [model.userinfo objectForKey:@"uname"];
        return cell;
    } else if (model.imgtotal != 0){
        PicOneTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
        [cell.coverImg setImageWithURL:[NSURL URLWithString:model.coverimg]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.titleLable.text = model.title;
        cell.introLable.text = model.content;
//        cell.picCount.text = [NSString stringWithFormat:@"%ldP", model.imgtotal];
        cell.typeLable.text = [NSString stringWithFormat:@"--%@--",model.name];
        return cell;
    } else {
        ReadStoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
        [cell.coverImg setImageWithURL:[NSURL URLWithString:model.coverimg]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.titleLable.text = model.title;
        cell.introLable.text = model.content;
        cell.typeLable.text = [NSString stringWithFormat:@"--%@--",model.name];
        return cell;
    }
}
/// tableView设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OnedayModel *model = [self.tableArr objectAtIndex:indexPath.row];
    if ([model.name isEqualToString:@"摄影"]||[model.name isEqualToString:@"插画"]) {
        return 340;
    } else if([model.name isEqualToString:@"音乐"]) {
    return 150;
    } else {
        return 260;
    }
}

/// tableView点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    OnedayModel *model = [self.tableArr objectAtIndex:indexPath.row];
    if ([model.name isEqualToString:@"Ting"]) {
        self.number = indexPath.row;
        MusicPlayViewController *musicVC = [[MusicPlayViewController alloc] init];
        musicVC.mid = model.id;
        musicVC.tableArray = self.playArr;
        musicVC.dic = model.playInfo;
        [self presentViewController:musicVC animated:YES completion:^{
        }];
        [musicVC release];
    } else if (model.playList.count != 0 || model.playList.count != 0) {
        
    }
    else if (model.playList.count == 0 && model.playList.count == 0) {
        ChoiceDetailViewController *infoVC = [[ChoiceDetailViewController alloc] init];
        infoVC.hidesBottomBarWhenPushed = YES;
        infoVC.contentid = model.id;
        [self.navigationController pushViewController:infoVC animated:YES];
        [infoVC release];
    }
}
/// 左边navBar, 跳转音乐播放页面
- (void)creatItem
{
    [self.tableView reloadData];
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    [button addTarget:self action:@selector(leftButtonItemAction:) forControlEvents:UIControlEventTouchUpInside];
        [button setBackgroundImage:[UIImage imageNamed:@"iconfont-ttpodicon.png"] forState:UIControlStateNormal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    [button release];
    
}
- (void)leftButtonItemAction:(id)sender
{
    OnedayModel *model = [self.tableArr objectAtIndex:self.number];
    MusicPlayViewController *musicVC = [[MusicPlayViewController alloc] init];
    [musicVC.tableArray removeAllObjects];
    musicVC.mid = model.id;
    musicVC.tableArray = self.playArr;
    musicVC.dic = model.playInfo;
    [self presentViewController:musicVC animated:YES completion:^{
        
    }];
    [musicVC release];
}



// 数据请求
- (void)requestData
{
    [AFNetWoringYu netWorkWithURL: @"http://api.pianke.me/today/newlist_v2" body: @"start=0&client=2&auth=XsRrEiWx8m35DDsV2HcbpjilwT5arNm2XZ8z9vWGMpbm1bw39ZOSBlZ4A&limit=10" resultBlock:^(id result) {
        NSDictionary *dic = [NSDictionary dictionaryWithDictionary:result];
        NSMutableArray *array = [[dic objectForKeyedSubscript:@"data"] objectForKey:@"list"];
        self.date = [[dic objectForKeyedSubscript:@"data"] objectForKey:@"date"];
        self.navigationItem.title = self.date;
        for (NSDictionary *myDic in array) {
            OnedayModel *model = [[OnedayModel alloc] init];
            [model setValuesForKeysWithDictionary:myDic];
            if (model.playInfo != nil) {
//                PlayInfoModel *playInfo = [[PlayInfoModel alloc] init];
//                [playInfo setValuesForKeysWithDictionary:model.playInfo];
                for (NSDictionary *playDic in model.playList) {
//                    PlayInfoModel *playInfo = [[PlayInfoModel alloc] init];
//                    [playInfo setValuesForKeysWithDictionary:playDic];
                    [self.playArr addObject:playDic];
            }
            }
            if (model.imglist != nil) {
                
                [self.imgArr removeAllObjects];
                [self.imgArr addObject:model.imglist];
            }
            [self.tableArr addObject:model];
        }
        [self.tableView reloadData];
    }];

}
///  下拉加载
- (void)headerRereshing
{
    // 添加下拉刷新头部控件
    [self.tableView addHeaderWithCallback:^{
    [AFNetWoringYu netWorkWithURL: @"http://api.pianke.me/today/newlist_v2" body: @"start=0&client=2&auth=XsRrEiWx8m35DDsV2HcbpjilwT5arNm2XZ8z9vWGMpbm1bw39ZOSBlZ4A&limit=10" resultBlock:^(id result) {
        [self.tableArr removeAllObjects];
        NSDictionary *dic = [NSDictionary dictionaryWithDictionary:result];
        NSMutableArray *array = [[dic objectForKeyedSubscript:@"data"] objectForKey:@"list"];
        for (NSDictionary *myDic in array) {
            OnedayModel *model = [[OnedayModel alloc] init];
            [model setValuesForKeysWithDictionary:myDic];
            if (model.playInfo != nil) {
//                PlayInfoModel *playInfo = [[PlayInfoModel alloc] init];
//                [playInfo setValuesForKeysWithDictionary:model.playInfo];
                for (NSDictionary *playDic in model.playList) {
//                    PlayInfoModel *playInfo = [[PlayInfoModel alloc] init];
//                    [playInfo setValuesForKeysWithDictionary:playDic];
                    [self.playArr addObject:playDic];
                }
            }
            if (model.imglist != nil) {
                
                [self.imgArr removeAllObjects];
                [self.imgArr addObject:model.imglist];
            }
            [self.tableArr addObject:model];
        }
        
        self.date = [[dic objectForKeyedSubscript:@"data"] objectForKey:@"date"];
        self.navigationItem.title = self.date;
        [self.tableView reloadData];
        [self.tableView headerEndRefreshing];
    }];
    }];
    
}
// 上拉刷新
- (void)footerRereshing
{
    self.start = 10;
    [self.tableView addFooterWithCallback:^{
    if (self.start < 100) {
        [AFNetWoringYu netWorkWithURL: @"http://api.pianke.me/today/newlist_v2" body: [NSString stringWithFormat:@"start=%ld&client=2&auth=XsRrEiWx8m35DDsV2HcbpjilwT5arNm2XZ8z9vWGMpbm1bw39ZOSBlZ4A&limit=10",self.start] resultBlock:^(id result) {
            NSDictionary *dic = [NSDictionary dictionaryWithDictionary:result];
            NSMutableArray *array = [[dic objectForKeyedSubscript:@"data"] objectForKey:@"list"];
            for (NSDictionary *myDic in array) {
                OnedayModel *model = [[OnedayModel alloc] init];
                [model setValuesForKeysWithDictionary:myDic];
                if (model.playInfo != nil) {
//                    PlayInfoModel *playInfo = [[PlayInfoModel alloc] init];
//                    [playInfo setValuesForKeysWithDictionary:model.playInfo];
                    for (NSDictionary *playDic in model.playList) {
//                        PlayInfoModel *playInfo = [[PlayInfoModel alloc] init];
//                        [playInfo setValuesForKeysWithDictionary:playDic];
                        [self.playArr addObject:playDic];
                    }
                }
                if (model.imglist != nil) {
                    
                    [self.imgArr removeAllObjects];
                    [self.imgArr addObject:model.imglist];
                }
                [self.tableArr addObject:model];
            }
            
            self.date = [[dic objectForKeyedSubscript:@"data"] objectForKey:@"date"];
            self.navigationItem.title = self.date;
            [self.tableView reloadData];
            _start = _start + 10;
            [self.tableView footerEndRefreshing];
        }];
    }
    }];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
